<?php

namespace App\Library\Traits;

use Illuminate\Support\Collection;

trait ImportProductTrait
{
    private function checkTitle(Collection $collection): bool
    {
        $array = $this->title();
        if (count($collection) !== count($array)) {
            $this->error('标题行数量错误,应为15行');
            return true;
        }
        foreach ($collection as $k => $v) {
            if ($v !== $array[$k]) {
                $this->error('标题行错误:' . chr(65 + $k) . '1' . '列应为:' . $array[$k]);
                return true;
            }
        }
        return false;
    }

    private function title(): array
    {
        return [
            '标题',
            '活动类型(数字)',
            '活动(0/1)0不参加/1参加',
            '野生/家养(0/1)',
            '保护动物(0/1)',
            '阶段(0/1/2)',
            '标签',
            '基因',
            '商品详情',
            '温馨提示',
            '规格名称',
            '规格原价',
            '规格库存',
            '规格限购数量',
            null,
        ];
    }

    private function error(string $error): void
    {
        $this->logic->status = 2;
        $this->logic->error = [$error];
        $this->logic->save();
    }

    private function hActivityType(mixed $field): int
    {
        if (is_null($field)) return 0;
        else return (int)$field;
    }

    private function hActivity(mixed $field): int
    {
        if (is_null($field)) return 0;
        else if ($field === 1) return 1;
        else return 0;
    }

    private function hWildCare(mixed $field): int
    {
        if (is_null($field)) return 0;
        else if ($field === 1) return 1;
        else if ($field === 2) return 1;
        else return 0;
    }

    private function hProtect(mixed $field): int
    {
        if (is_null($field)) return 0;
        else if ($field === 1) return 1;
        else return 0;
    }

    private function hStage(mixed $field): int
    {
        if (is_null($field)) return 0;
        else if ($field === 1) return 1;
        else if ($field === 2) return 1;
        else return 0;
    }

    private function hLabel(mixed $field): ?string
    {
        if (is_null($field)) return null;
        $field = htmlspecialchars(strip_tags(trim($field)));
        if (empty($field)) return null;
        else return $field;
    }

    private function hGene(mixed $field): ?string
    {
        if (is_null($field)) return null;
        $field = htmlspecialchars(strip_tags(trim($field)));
        if (empty($field)) return null;
        else return $field;
    }

    private function hContent(mixed $field): ?string
    {
        if (is_null($field)) return null;
        $field = htmlspecialchars(strip_tags(trim($field)));
        if (empty($field)) return null;
        else return $field;
    }

    private function hNote(mixed $field): ?string
    {
        if (is_null($field)) return null;
        $field = htmlspecialchars(strip_tags(trim($field)));
        if (empty($field)) return null;
        else return $field;
    }

    private function hPsName(mixed $field): string
    {
        if (is_null($field)) return '默认';
        $field = htmlspecialchars(strip_tags(trim($field)));
        if (empty($field)) return '默认';
        else return $field;
    }

    private function hPsPriceUnderlined(mixed $field): float
    {
        if (is_null($field)) return 0.01;
        $field = htmlspecialchars(strip_tags(trim($field)));
        if (empty($field)) return 0.01;
        else return $field * 1;
    }

    private function hPsUsableStock(mixed $field): int
    {
        if (is_null($field)) return 0;
        $field = htmlspecialchars(strip_tags(trim($field)));
        if (empty($field)) return 0;
        else return (int)$field;
    }

    private function hPsBuyLimit(mixed $field): int
    {
        if (is_null($field)) return 0;
        $field = htmlspecialchars(strip_tags(trim($field)));
        if (empty($field)) return 0;
        else return (int)$field;
    }
}
